package com.iot.inclinometer.dao.impl;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.iot.inclinometer.dao.IRealInfoDao;
import com.iot.inclinometer.db.DBHelper;
import com.iot.inclinometer.pojo.RealInfo;

import java.util.ArrayList;
import java.util.List;

public class RealInfoDaoImpl implements IRealInfoDao{
	private DBHelper helper;

	public RealInfoDaoImpl(Context context) {
		helper = new DBHelper(context);
	}
	
	@Override
	public boolean insertAll(List<RealInfo> allDatas) {
		boolean flag = false;
		SQLiteDatabase database = helper.getWritableDatabase();
		database.beginTransaction();// 开始事务
		String sql = "INSERT INTO real_data_info(theta_A,theta_B,ltheta_A,ltheta_B,lltheta_A,lltheta_B,currentMeasureTag,ob_id) VALUES (?,?,?,?,?,?,?,?);";
		try {
			// 循环将数据存入数据库中
			for (RealInfo realInfo : allDatas) {
				database.execSQL(sql, new Object[] { realInfo.getLtheta_A(),
						realInfo.getTheta_B(),realInfo.getLtheta_A(),realInfo.getLtheta_B(),realInfo.getLltheta_A(),
						realInfo.getLltheta_B(),realInfo.getCurrentMeasureTag(),realInfo.getObserverId()});
			}
			database.setTransactionSuccessful();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			database.endTransaction();
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}
	/*String sql6 = "CREATE TABLE IF NOT EXISTS real_data_info " 
	+"(real_id INTEGER PRIMARY KEY AUTOINCREMENT,theta_A VARCHAR(64),"
	+"theta_B VARCHAR(64),ltheta_A VARCHAR(64),ltheta_B VARCHAR(64),lltheta_A VARCHAR(64),"
	+"lltheta_B VARCHAR(64),ob_id INTEGER)" ;*/
	@Override
	public int add(RealInfo realInfo) {
		int _id = 0;
		SQLiteDatabase database = null;
		try {
			String sql1 = "INSERT INTO real_data_info(theta_A,theta_B,ltheta_A,ltheta_B,lltheta_A,lltheta_B,currentMeasureTag,ob_id) VALUES (?,?,?,?,?,?,?,?);";
			database = helper.getWritableDatabase();
			database.execSQL(
					sql1,
					new Object[] { realInfo.getTheta_A(),realInfo.getTheta_B(),realInfo.getLtheta_A(),
							realInfo.getLtheta_B(),realInfo.getLltheta_A(),realInfo.getLltheta_B(),realInfo.getCurrentMeasureTag(),realInfo.getObserverId()});

			String sql2 = "select * from real_data_info where theta_A=? and theta_B=? and ltheta_A=?";

			Cursor cursor = database.rawQuery(
					sql2,
					new String[] { realInfo.getTheta_A(),realInfo.getTheta_B(),realInfo.getLtheta_A()});
			while (cursor.moveToNext()) {
				_id = cursor.getInt(cursor.getColumnIndex("real_id"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (database != null) {
				database.close();
			}
		}
		Log.i("info", "插入的数据的ID是：" + _id);
		return _id;
	}

	
	@Override
	public List<RealInfo> selectRealInfos() {
		List<RealInfo> allDataInfos = new ArrayList<RealInfo>();
		SQLiteDatabase database = null;
		
		try {
			String sql = "select * from real_data_info";
			database = helper.getReadableDatabase();
			Cursor cursor = database.rawQuery(sql,null);
			while (cursor.moveToNext()) {
				int _id = cursor.getInt(cursor.getColumnIndex("real_id"));
				int _obId = cursor.getInt(cursor.getColumnIndex("ob_id"));
				String theta_A = cursor
						.getString(cursor.getColumnIndex("theta_A"));
				String theta_B = cursor
						.getString(cursor.getColumnIndex("theta_B"));
				String ltheta_A = cursor
						.getString(cursor.getColumnIndex("ltheta_A"));
				String ltheta_B = cursor
						.getString(cursor.getColumnIndex("ltheta_B"));
				String lltheta_A = cursor
						.getString(cursor.getColumnIndex("lltheta_A"));
				String lltheta_B = cursor
						.getString(cursor.getColumnIndex("lltheta_B"));
				String currentMeasureTag = cursor
						.getString(cursor.getColumnIndex("currentMeasureTag"));
				
				RealInfo info = new RealInfo();
				info.setRealId(_id);
				info.setObserverId(_obId);
				info.setTheta_A(theta_A);
				info.setTheta_B(theta_B);
				info.setLtheta_A(ltheta_A);
				info.setLtheta_B(ltheta_B);
				info.setLltheta_A(lltheta_A);
				info.setLltheta_B(lltheta_B);
				info.setCurrentMeasureTag(currentMeasureTag);
				allDataInfos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return allDataInfos;
	}
	
	@Override
	public List<RealInfo> selectRealInfos(int obId) {

		List<RealInfo> allDataInfos = new ArrayList<RealInfo>();

		SQLiteDatabase database = helper.getReadableDatabase();
		String sql = "select * from real_data_info where ob_id = ?";
		try {
			Cursor cursor = database.rawQuery(sql,
					new String[] { Integer.toString(obId) });

			while (cursor.moveToNext()) {

				int _id = cursor.getInt(cursor.getColumnIndex("real_id"));
				int _obId = cursor.getInt(cursor.getColumnIndex("ob_id"));
				String theta_A = cursor
						.getString(cursor.getColumnIndex("theta_A"));
				String theta_B = cursor
						.getString(cursor.getColumnIndex("theta_B"));
				String ltheta_A = cursor
						.getString(cursor.getColumnIndex("ltheta_A"));
				String ltheta_B = cursor
						.getString(cursor.getColumnIndex("ltheta_B"));
				String lltheta_A = cursor
						.getString(cursor.getColumnIndex("lltheta_A"));
				String lltheta_B = cursor
						.getString(cursor.getColumnIndex("lltheta_B"));
				String currentMeasureTag = cursor
						.getString(cursor.getColumnIndex("currentMeasureTag"));
				
				RealInfo info = new RealInfo();
				info.setRealId(_id);
				info.setObserverId(_obId);
				info.setTheta_A(theta_A);
				info.setTheta_B(theta_B);
				info.setLtheta_A(ltheta_A);
				info.setLtheta_B(ltheta_B);
				info.setLltheta_A(lltheta_A);
				info.setLltheta_B(lltheta_B);
				info.setCurrentMeasureTag(currentMeasureTag);
				allDataInfos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return allDataInfos;
	}
	@Override
	public List<RealInfo> selectRealInfos(int obId,String currentMeasureTag) {

		List<RealInfo> allDataInfos = new ArrayList<RealInfo>();

		SQLiteDatabase database = helper.getReadableDatabase();
		String sql = "select * from real_data_info where ob_id = ? and currentMeasureTag=?";
		try {
			Cursor cursor = database.rawQuery(sql,
					new String[] { Integer.toString(obId),currentMeasureTag });

			while (cursor.moveToNext()) {

				int _id = cursor.getInt(cursor.getColumnIndex("real_id"));
				int _obId = cursor.getInt(cursor.getColumnIndex("ob_id"));
				String theta_A = cursor
						.getString(cursor.getColumnIndex("theta_A"));
				String theta_B = cursor
						.getString(cursor.getColumnIndex("theta_B"));
				String ltheta_A = cursor
						.getString(cursor.getColumnIndex("ltheta_A"));
				String ltheta_B = cursor
						.getString(cursor.getColumnIndex("ltheta_B"));
				String lltheta_A = cursor
						.getString(cursor.getColumnIndex("lltheta_A"));
				String lltheta_B = cursor
						.getString(cursor.getColumnIndex("lltheta_B"));
				String currentMeasureTag1 = cursor
						.getString(cursor.getColumnIndex("currentMeasureTag"));
				
				RealInfo info = new RealInfo();
				info.setRealId(_id);
				info.setObserverId(_obId);
				info.setTheta_A(theta_A);
				info.setTheta_B(theta_B);
				info.setLtheta_A(ltheta_A);
				info.setLtheta_B(ltheta_B);
				info.setLltheta_A(lltheta_A);
				info.setLltheta_B(lltheta_B);
				info.setCurrentMeasureTag(currentMeasureTag1);
				allDataInfos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return allDataInfos;
	}
	
	/**
	 * 通过obId删除测量结果数据
	 */
	@Override
	public int deleteRealInfosByobId(int obId){
		//得到可写的SQLiteDatabase对象  
        SQLiteDatabase database = helper.getWritableDatabase();  
        //调用delete方法，删除数据  
        int flag=database.delete("real_data_info", "ob_id=?", new String[]{Integer.toString(obId)});  
        return flag;
	}

	@Override
	public int deleteAllRealInfos() {
		//得到可写的SQLiteDatabase对象  
        SQLiteDatabase database = helper.getWritableDatabase();  
        String sql = "delete from real_data_info";
        database.execSQL(sql);
        database.close();
        //调用delete方法，删除数据  
        int flag=1;
        return flag;
	}
}
